package com.store.practice.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.store.practice.common.api.CommonResult;
import com.store.practice.model.AppAccount;
import com.store.practice.service.BeauticianUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "AccountController",description = "美容师登录注册管理")
@RequestMapping("/account")
@RestController
public class AccountController {
    @Autowired
    private BeauticianUserService userService;

    @ApiOperation("美容师登录")
    @PostMapping("/login")
    public CommonResult login(Long num,String password){
        Map result = userService.login(num, password);
        if (result!=null){
            return CommonResult.success(result);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取短信验证码")
    @PostMapping("/getCode")
    public CommonResult getCode(Long phone){
        String result = userService.getCode(phone);
        if (StrUtil.equals("success",result)){
            return CommonResult.success(result);
        }
        return CommonResult.failed(result);
    }

    @ApiOperation("注册账户")
    @PostMapping("/register")
    public CommonResult register(Long phone,String code,String password) {
        int result = userService.register(phone, code, password);
        if (result==-1){
            return CommonResult.failed("手机号已修改");
        }
        if (result==-2){
            return CommonResult.failed("验证码错误");
        }
        if (result==-3){
            return CommonResult.failed("验证码已失效");
        }
        return CommonResult.success(result);
    }

    @ApiOperation("忘记密码")
    @PostMapping("/reset")
    public CommonResult reset(Long account,String password1,String password2){
        int i = userService.reset(account, password1, password2);
        if (i==-1){
            return CommonResult.failed("该账号不存在");
        }
        if (i==-2){
            return CommonResult.failed("密码不一致");
        }
        return CommonResult.success(i);
    }


}
